package com.taikang.hmp.hbusiness.diabetes.measure.weight.action.intface;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.taikang.hmp.frame.common.ErrorConstant;
import com.taikang.hmp.frame.common.TKException;
import com.taikang.hmp.frame.common.datastructre.Dto;
import com.taikang.hmp.frame.common.datastructre.support.BaseDto;
import com.taikang.hmp.frame.common.datastructre.support.ResponseDto;
import com.taikang.hmp.frame.common.validate.RequestItemCount;
import com.taikang.hmp.frame.common.web.action.BaseAction;
import com.taikang.hmp.frame.common.web.session.AppSessionFactory;
import com.taikang.hmp.frame.common.web.session.SessionBean;
import com.taikang.hmp.hbusiness.diabetes.measure.weight.service.IWeightService;

/** 
 * 体重
 * author:qrp
 * time:2015年8月5日 上午10:56:01
 */
public class WeightAction extends BaseAction{
	private static Logger logger = LoggerFactory.getLogger(WeightAction.class);
	
	private IWeightService weightService;

	public IWeightService getWeightService() {
		return weightService;
	}

	public void setWeightService(IWeightService weightService) {
		this.weightService = weightService;
	}

	public String executeAction()  {
		if ("saveWeight".equals(requestDto.getMethod())) {
			logger.info("--保存糖化血红蛋白测量值 saveHemoglobin method -- ");
			return saveWeight();
		}else if("getWeightList".equals(requestDto.getMethod())){
			logger.info("--保存糖化血红蛋白测量值 saveHemoglobin method -- ");
			return getWeightList();
		}else{
			throw new TKException("no support method:"+requestDto.getMethod());
		}
	}
	
	/**
	 * 保存体重测量值
	 * 
	 * @return
	 */
	public String saveWeight() {
		logger.info("进入saveWeight方法:"+requestDto.toString());
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		String sessionId = requestDto.getSessionid();
		SessionBean sessionBean = AppSessionFactory.getInstance(requestDto.getRequestsource()).isSessionTimeout(sessionId);
		if (sessionBean != null) {
			paramDto.put("height", sessionBean.getCustomerInfo().getHeight());//找到其身高，计算BMI
			String result = weightService.saveWeight(paramDto);
			if(result!=null){
				Dto resultDto = new BaseDto();
				resultDto.put("result", result);
				responseDto.pushResultDto(resultDto);
			}else{
				responseDto.addErrorCode(ErrorConstant.HD_NO_PERSONAL_INFO);
			}
			
		} else {
			responseDto.addErrorCode(ErrorConstant.SESSION_TIMEOUT);
		}
		super.setDataMap(responseDto);
		logger.info("saveWeight成功返回结果:"+responseDto.toString());
		return SUCCESS;
	}
	/**
	 * 删除体重测量值
	 * @return
	 */
	public String delWeight(){
		return SUCCESS;
	}

	/**
	 * 获取体重测量值列表
	 * 
	 * @return
	 */
	public String getWeightList() {
		logger.info("进入getWeightList方法:"+requestDto.toString());
		ResponseDto responseDto = new ResponseDto();
		//获取参数
		Dto paramDto = requestDto.getRequestlist().get(0);
		paramDto = super.appendRequestBase(paramDto, requestDto);
		String sessionId = requestDto.getSessionid();
		SessionBean sessionBean = AppSessionFactory.getInstance(requestDto.getRequestsource()).isSessionTimeout(sessionId);
		if (sessionBean != null) {
			paramDto.put("height", sessionBean.getCustomerInfo().getHeight());//找到其身高，计算BMI
			List<Dto> resultList = weightService.getWeightList(paramDto);
			Dto resultDto = new BaseDto();
			resultDto.put("resultlist", resultList);
			responseDto.pushResultDto(resultDto);
		} else {
			responseDto.addErrorCode(ErrorConstant.SESSION_TIMEOUT);
		}
		super.setDataMap(responseDto);
		logger.info("getWeightList方法成功返回结果:"+responseDto.toString());
		return SUCCESS;
	}
}
